package com.cheng.student.controller.servlet;

import com.cheng.student.beans.Student;
import com.cheng.student.service.StudentServiceImpl;
import com.cheng.student.service.interfaces.StudentService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.*;

@WebServlet(name = "StudentLoginServlet", value = "/StudentLogin")
public class StudentLoginServlet
        extends HttpServlet
{
    private static String successHtml = "";

    static
    {
        FileInputStream fileInputStream = null;
        try
        {
            fileInputStream = new FileInputStream(
                    new File("src/main/webapp/stuQuery.html"));
            byte[] b = new byte[512];
            int i = 0;
            while((i = fileInputStream.read(b)) != -1)
            {
                successHtml += new String(b,0,i);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        //考生号
        String examStudentNumber = request.getParameter("examStudentNumber");
        //姓名
        String studentName = request.getParameter("StudentName");
        //验证码
        String checkCode = request.getParameter("checkCode");
        //设置字符集
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //校验合理性

        if (studentName == null || "".equals(studentName))
        {
            response.setHeader("loginInformation","nameNull");
//            response.getWriter().write("姓名不可以为空");
            return;
        }
        if (examStudentNumber == null || "".equals(examStudentNumber))
        {
            response.setHeader("loginInformation","numberNull");

//            response.getWriter().write("请输入考生号");
            return;
        }
        if(checkCode == null || "".equals(checkCode))
        {
            response.setHeader("loginInformation","checkCodeNull");

//            response.getWriter().write("请输入验证码");
            return;
        }
        else
        {
            if (!checkCode.equals(request.getSession().getAttribute("verificationCode")))
            {
                response.setHeader("loginInformation","checkCodeWrong");

//                response.getWriter().write("验证码错误");
                return;
            }
        }
        //通过
        request.getSession().removeAttribute("verificationCode");
        //尝试登录
        StudentService studentService = new StudentServiceImpl();
        Student student = studentService.studentLogin(studentName, examStudentNumber);
        if(student == null)
        {
            response.setHeader("loginInformation","noSuchStudent");

//            response.getWriter().write("姓名或考生号错误，登陆失败");
            return;
        }
        response.setHeader("loginInformation","congratulations");

        String loginSuccess = successHtml;

        //html解析信息
        Document parse = Jsoup.parse(loginSuccess);
        //恭喜信息
        Element congratulations = parse.getElementById("congratulations");
        //学生姓名
        Element name = parse.getElementById("studentName");
        //学生性别
        Element studentGender = parse.getElementById("studentGender");
        //考生号
        Element studentExamNumber = parse.getElementById("studentExamNumber");
        //身份证号
        Element idNumber = parse.getElementById("IDNumber");
        //录取学院
        Element admitAcademy = parse.getElementById("admitAcademy");
        //录取专业
        Element admitSpecialty = parse.getElementById("admitSpecialty");
        //录取批次
        Element admitBatch = parse.getElementById("admitBatch");
        //科类
        Element subjectType = parse.getElementById("subjectType");
        //邮件单号
        Element ems = parse.getElementById("EMS");

        //赋值
        congratulations.html("恭喜考生" +
                student.getXM() +
                "被我校" +
                student.getZYMC() +
                "专业录取");

        name.html(student.getXM());

        studentGender.html(student.getXBMC());

        studentExamNumber.html(student.getKSH());

        idNumber.html(student.getSFZH());

        admitAcademy.html(student.getFYXB());

        admitSpecialty.html(student.getZYMC());

        admitBatch.html(student.getPCMC());

        subjectType.html(student.getKLMC());

        ems.html(student.getEMS());

        parse.html();

        response.getWriter().write(parse.html());
        return;

    }
}
